﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Web;
using System.Globalization;
using System.Web.UI;
using Microsoft.SharePoint.WebPartPages;

namespace LifeInSharePoint.Common
{
    public class SPFormHelper
    {
        private const string STYLE_FORMTABLE = "ics_formTable";
        private const string STYLE_LABELCELL = "ics_formlabel";
        private const string STYLE_TEXTBOXCELL = "ics_formbody";
        private const string STYLE_TEXTBOXWIDE = "ics_long";
        private const string STYLE_BUTTON = "ics_ButtonHeightWidth";
        private const string STYLE_FORMLINE = "ics_formline";
        private const string STYLE_BUTTONTABLE = "ics_formtoolbar";
        private const string STYLE_BUTTONCELL = "ics_toolbar";
        private const string STYLE_VALIDATORCELL = "ics_validator";
        private const string STYLE_HEADERCELL = "ics_tableheader";

        public static void AddRichTextRow(Table table, string label, string description, RequiredFieldValidator reqValidator, RegularExpressionValidator regValidator, InputFormTextBox newControl, SPWebPartManager webPartManager)
        {
            TableRow row = new TableRow();
            table.Rows.Add(row);

            TableCell labelCell = new TableCell();
            labelCell.Attributes.Add("class", STYLE_LABELCELL);
            labelCell.RowSpan = 2;
            row.Cells.Add(labelCell);

            Label newLabel = new Label();
            if (reqValidator != null && webPartManager.DisplayMode == SPWebPartManager.BrowseDisplayMode)
            {
                newLabel.Text = "<label>" + label + "<span style='color:#8a1f11;'>*</span></label><span class='small'>" + description + "</span>";
            }
            else
            {
                newLabel.Text = "<label>"+ label +"</label><span class='small'>" + description + "</span>";
            }
            labelCell.Controls.Add(newLabel);

            TableCell controlCell = new TableCell();
            controlCell.Attributes.Add("class", STYLE_TEXTBOXCELL);
            controlCell.ColumnSpan = 2;
            row.Cells.Add(controlCell);
            newControl.Attributes.Add("class", STYLE_TEXTBOXWIDE);

            TableRow validatorRow = new TableRow();
            table.Rows.Add(validatorRow);

            TableCell validatorCell = new TableCell();
            validatorCell.Attributes.Add("class", STYLE_VALIDATORCELL);
            validatorCell.ColumnSpan = 2;
            validatorRow.Cells.Add(validatorCell);

            if (reqValidator != null && webPartManager.DisplayMode == SPWebPartManager.BrowseDisplayMode)
            {
                reqValidator.CssClass = "error";
                validatorCell.Controls.Add(reqValidator);
            }
            controlCell.Controls.Add(newControl);

        }

        public static void AddUnediableRow(Table table, string label, string description, string value)
        {
            TableRow row = new TableRow();
            table.Rows.Add(row);

            TableCell labelCell = new TableCell();
            labelCell.Attributes.Add("class", STYLE_LABELCELL);
            row.Cells.Add(labelCell);

            Label newLabel = new Label();
            newLabel.Text = "<label>"+ label +"</label><span class='small'>" + description + "</span>";
            labelCell.Controls.Add(newLabel);

            TableCell controlCell = new TableCell();
            controlCell.Attributes.Add("class", STYLE_TEXTBOXCELL);
            row.Cells.Add(controlCell);

            Label newLabel2 = new Label();
            newLabel2.Text = label + ":";
            controlCell.Controls.Add(newLabel2);

            controlCell.Controls.Add(newLabel2);
            controlCell.ColumnSpan = 2;


            TableCell validatorCell = new TableCell();
            validatorCell.Attributes.Add("class", STYLE_VALIDATORCELL);
            row.Cells.Add(validatorCell);


        }


        public static void AddLabelledRow(Table table, string label, string description, TextBox newControl, RequiredFieldValidator reqValidator, RegularExpressionValidator regValidator, bool active, SPWebPartManager webPartManager)
        {
            TableRow row = new TableRow();
            table.Rows.Add(row);

            TableCell labelCell = new TableCell();
            labelCell.Attributes.Add("class", STYLE_LABELCELL);
            labelCell.RowSpan = 2;
            row.Cells.Add(labelCell);

            Label newLabel = new Label();
            if (reqValidator != null && webPartManager.DisplayMode == SPWebPartManager.BrowseDisplayMode)
            {
                newLabel.Text = "<label>" + label + "<span style='color:#8a1f11;'>*</span></label><span class='small'>" + description + "</span>";
            }
            else
            {
                newLabel.Text = "<label>"+ label +"</label><span class='small'>" + description + "</span>";
            }
            labelCell.Controls.Add(newLabel);

            TableCell controlCell = new TableCell();
            controlCell.Attributes.Add("class", STYLE_TEXTBOXCELL);
            row.Cells.Add(controlCell);
            newControl.Attributes.Add("class", STYLE_TEXTBOXWIDE);
            newControl.ReadOnly = active;
            controlCell.Controls.Add(newControl);
            controlCell.ColumnSpan = 2;

            TableRow validatorRow = new TableRow();
            table.Rows.Add(validatorRow);

            TableCell validatorCell = new TableCell();
            validatorCell.Attributes.Add("class", STYLE_VALIDATORCELL);
            validatorCell.ColumnSpan = 2;
            validatorRow.Cells.Add(validatorCell);

            if (reqValidator != null && webPartManager.DisplayMode == SPWebPartManager.BrowseDisplayMode)
            {
                reqValidator.CssClass = "error";
                validatorCell.Controls.Add(reqValidator);
            }

        }

        public static void AddCheckBoxListRow(Table table, CheckBoxList newControl, RequiredFieldValidator reqValidator, RegularExpressionValidator regValidator, string label, string description, SPWebPartManager webPartManager)
        {
            TableRow row = new TableRow();
            table.Rows.Add(row);

            TableCell labelCell = new TableCell();
            labelCell.Attributes.Add("class", STYLE_LABELCELL);
            row.Cells.Add(labelCell);

            Label newLabel = new Label();
            newLabel.Text = "<label>"+ label +"</label><span class='small'>" + description + "</span>";
            labelCell.Controls.Add(newLabel);

            TableCell controlCell = new TableCell();
            controlCell.Attributes.Add("class", STYLE_TEXTBOXCELL);
            row.Cells.Add(controlCell);

            controlCell.Controls.Add(newControl);

            TableCell validatorCell = new TableCell();
            validatorCell.Attributes.Add("class", STYLE_VALIDATORCELL);

            row.Cells.Add(validatorCell);
        }

        public static void AddCheckBoxRow(Table table, CheckBox newControl, RequiredFieldValidator reqValidator, RegularExpressionValidator regValidator, string label, string description, SPWebPartManager webPartManager)
        {
            TableRow row = new TableRow();
            table.Rows.Add(row);

            TableCell labelCell = new TableCell();
            labelCell.Attributes.Add("class", STYLE_LABELCELL);
            row.Cells.Add(labelCell);

            Label newLabel = new Label();
            newLabel.Text = "<label>"+ label +"</label><span class='small'>" + description + "</span>";
            labelCell.Controls.Add(newLabel);

            TableCell controlCell = new TableCell();
            controlCell.Attributes.Add("class", STYLE_TEXTBOXCELL);
            row.Cells.Add(controlCell);

            controlCell.Controls.Add(newControl);

            TableCell validatorCell = new TableCell();
            validatorCell.Attributes.Add("class", STYLE_VALIDATORCELL);

            row.Cells.Add(validatorCell);
        }

        public static void AddHeadingRow(Table table, string label)
        {
            TableRow row = new TableRow();
            table.Rows.Add(row);

            TableHeaderCell labelCell = new TableHeaderCell();
            labelCell.Attributes.Add("class", STYLE_HEADERCELL);
            row.Cells.Add(labelCell);


            HtmlGenericControl newLabel = new HtmlGenericControl("h1");
            newLabel.InnerText = label;
            labelCell.Controls.Add(newLabel);
            labelCell.ColumnSpan = 4;
        }

        public static void AddTextRow(Table table, string label, string description, TextBox FieldLabel, SPWebPartManager webPartManager)
        {
            TableRow row = new TableRow();
            table.Rows.Add(row);


            TableCell labelCell = new TableCell();
            labelCell.Attributes.Add("class", STYLE_LABELCELL);
            row.Cells.Add(labelCell);

            Label newLabel = new Label();
            newLabel.Text = "<label>"+ label +"</label><span class='small'>" + description + "</span>";
            labelCell.Controls.Add(newLabel);

            TableCell controlCell = new TableCell();
            controlCell.Attributes.Add("class", STYLE_TEXTBOXCELL);
            row.Cells.Add(controlCell);
            controlCell.ColumnSpan = 3;

            controlCell.Controls.Add(FieldLabel);
        }

        public static void AddPeoplePickerRow(Table table, string label, string description, PeopleEditor newControl, SPWebPartManager webPartManager)
        {
            TableRow row = new TableRow();
            table.Rows.Add(row);


            TableCell labelCell = new TableCell();
            labelCell.Attributes.Add("class", STYLE_LABELCELL);
            row.Cells.Add(labelCell);

            Label newLabel = new Label();
            newLabel.Text = "<label>"+ label +"</label><span class='small'>" + description + "</span>";
            labelCell.Controls.Add(newLabel);

            TableCell controlCell = new TableCell();
            controlCell.Attributes.Add("class", STYLE_TEXTBOXCELL);
            row.Cells.Add(controlCell);
            controlCell.ColumnSpan = 3;

            HtmlGenericControl _wrapper = new HtmlGenericControl("div");
            _wrapper.Attributes.Add("class", "PeoplePickerWrapper");
            _wrapper.Controls.Add(newControl);
            controlCell.Controls.Add(_wrapper);
        }

        public static void AddDropDownRow(Table table, string label, string description, RequiredFieldValidator reqValidator, RegularExpressionValidator regValidator, DropDownList newControl, SPWebPartManager webPartManager)
        {
            TableRow row = new TableRow();
            table.Rows.Add(row);

            TableCell labelCell = new TableCell();
            labelCell.Attributes.Add("class", STYLE_LABELCELL);
            labelCell.RowSpan = 2;
            row.Cells.Add(labelCell);

            Label newLabel = new Label();
            if (reqValidator != null && webPartManager.DisplayMode == SPWebPartManager.BrowseDisplayMode)
            {
                newLabel.Text = "<label>" + label + "<span style='color:#8a1f11;'>*</span></label><span class='small'>" + description + "</span>";
            }
            else
            {
                newLabel.Text = "<label>"+ label +"</label><span class='small'>" + description + "</span>";
            }
            labelCell.Controls.Add(newLabel);

            TableCell controlCell = new TableCell();
            controlCell.Attributes.Add("class", STYLE_TEXTBOXCELL);
            row.Cells.Add(controlCell);
            controlCell.ColumnSpan = 2;


            TableRow validatorRow = new TableRow();
            table.Rows.Add(validatorRow);

            TableCell validatorCell = new TableCell();
            validatorCell.Attributes.Add("class", STYLE_VALIDATORCELL);
            validatorCell.ColumnSpan = 2;
            validatorRow.Cells.Add(validatorCell);

            if (reqValidator != null && webPartManager.DisplayMode == SPWebPartManager.BrowseDisplayMode)
            {
                reqValidator.CssClass = "error";
                validatorCell.Controls.Add(reqValidator);
            }
            controlCell.Controls.Add(newControl);

        }

        public static void AddButtonRow(Table table, string label, Button newControl)
        {
            TableRow row = new TableRow();
            table.Rows.Add(row);

            TableCell controlCell = new TableCell();
            controlCell.Attributes.Add("class", STYLE_TEXTBOXCELL);
            row.Cells.Add(controlCell);
            controlCell.ColumnSpan = 4;
            newControl.Text = label;

            controlCell.Controls.Add(newControl);

        }

        public static void AddImageButtonRow(Table table, string imageUrl, ImageButton newControl)
        {
            TableRow row = new TableRow();
            table.Rows.Add(row);

            TableCell controlCell = new TableCell();
            controlCell.Attributes.Add("class", STYLE_TEXTBOXCELL);
            row.Cells.Add(controlCell);
            controlCell.ColumnSpan = 5;
            newControl.ImageUrl = imageUrl;

            controlCell.Controls.Add(newControl);

        }

    }
}
